package routers

import (
	"fmt"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/tidwall/sjson"

	"hass.tree.com/bridge/biz/wsmsg"
	"hass.tree.com/bridge/vo"
)

func LoadPublic(e *gin.Engine) {
	publicRoute := e.Group("/public")
	{
		publicRoute.POST("/upload", uploadFile)
		publicRoute.POST("/testws", testWebsocket)
	}

}
func uploadFile(c *gin.Context) {
	var fileData vo.Base64File
	if err := c.ShouldBindJSON(&fileData); err != nil {
		vo.Fail(err.Error(), c)
		return
	}
	fmt.Printf("data:image/jpg;base64,%s\n", fileData.Data)
	vo.Success("ok", c)
}

func testWebsocket(c *gin.Context) {
	data, _ := c.GetRawData()
	milliseconds := time.Now().UnixNano() / int64(time.Millisecond)
	sendStr, _ := sjson.Set(string(data), "id", milliseconds)
	fmt.Printf("%s\n", sendStr)
	wsmsg.SendMsg(*wsmsg.HassWsHelper, sendStr)

	vo.Success("ok", c)
}
